package com.lzm.ydpt.module.order.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.byt.framlib.base.BaseActivity;
import com.byt.framlib.baseadapter.rv.RvCommonAdapter;
import com.byt.framlib.baseadapter.rv.RvViewHolder;
import com.byt.framlib.basemvp.BasePresenter;
import com.byt.framlib.commonwidget.NormalTitleBar;
import com.byt.framlib.commonwidget.OnNoDoubleClickListener;
import com.byt.framlib.commonwidget.onclicktwo.AntiShake;
import com.lzm.ydpt.Constants;
import com.lzm.ydpt.R;
import com.lzm.ydpt.entity.GratuityBean;
import com.lzm.ydpt.module.order.view.GridSpacingItemDecoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 加小费
 */
public class AddGratuityActivity extends BaseActivity {

    @BindView(R.id.ntb_add_gratuity)
    NormalTitleBar ntb_add_gratuity;
    @BindView(R.id.rv_add_gratuity)
    RecyclerView rv_add_gratuity;
    @BindView(R.id.rl_gratuity_content)
    RelativeLayout rl_gratuity_content;
    @BindView(R.id.ed_add_gratuity_content)
    EditText ed_add_gratuity_content;

    private float gratuity = 0f;
    private List<GratuityBean> mGratuityList = new ArrayList<>();
    private RvCommonAdapter<GratuityBean> mGratuityAdapter = null;
    private GratuityBean selectGratuityBean = null;

    @Override
    public int getLayoutId() {
        return R.layout.activity_add_gratuity;
    }

    @Override
    public BasePresenter initPreData() {
        return null;
    }

    @Override
    public void initView() {
        initTitle();
        gratuity = getIntent().getFloatExtra(Constants.INP_GRATUITY_VALUE, 0f);
        mGratuityList.clear();
        mGratuityList.addAll(Constants.fetGratuityCondition());
        if (gratuity > 0) {
            boolean isOther = true;
            for (GratuityBean temPgratuity : mGratuityList) {
                if (temPgratuity.getGratuity() == gratuity) {
                    selectGratuityBean = temPgratuity;
                    temPgratuity.setSelect(true);
                    isOther = false;
                    break;
                }
            }
            if (isOther) {
                selectGratuityBean = mGratuityList.get(mGratuityList.size() - 1);
                mGratuityList.get(mGratuityList.size() - 1).setSelect(true);
                rl_gratuity_content.setVisibility(View.VISIBLE);
                ed_add_gratuity_content.setText(String.valueOf(gratuity));
            } else {
                rl_gratuity_content.setVisibility(View.GONE);
            }
        }
        mGratuityAdapter = new RvCommonAdapter<GratuityBean>(mBContext, mGratuityList, R.layout.item_gratuity_tag_layout) {
            @Override
            protected void convert(RvViewHolder holder, GratuityBean gratuityBean, int position) {
                holder.setText(R.id.tv_goods_type_name, gratuityBean.getGratuity_name());
                holder.setSelected(R.id.tv_goods_type_name, gratuityBean.isSelect());
                holder.getConvertView().setOnClickListener(new OnNoDoubleClickListener() {
                    @Override
                    protected void onNoDoubleClick(View v) {
                        if (gratuityBean.getGratuity() == -1) {
                            rl_gratuity_content.setVisibility(View.VISIBLE);
                        } else {
                            rl_gratuity_content.setVisibility(View.GONE);
                        }
                        selectGratuityBean = gratuityBean;
                        for (GratuityBean tempGratuity : mGratuityList) {
                            tempGratuity.setSelect(tempGratuity.equals(gratuityBean));
                        }
                        notifyDataSetChanged();
                    }
                });
            }
        };
        GridLayoutManager goodsManager = new GridLayoutManager(mBContext, 3, GridLayoutManager.VERTICAL, false);
        rv_add_gratuity.setLayoutManager(goodsManager);
        rv_add_gratuity.setNestedScrollingEnabled(false);
        rv_add_gratuity.addItemDecoration(new GridSpacingItemDecoration(3, getResources().getDimensionPixelSize(R.dimen.x10), true));
        rv_add_gratuity.setAdapter(mGratuityAdapter);
    }

    private void initTitle() {
        setNtbTitle(ntb_add_gratuity, true);
        ntb_add_gratuity.setTitleText("加小费");
        ntb_add_gratuity.setOnBackListener(new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                finish();
            }
        });
    }

    @OnClick({R.id.tv_add_gratuity})
    public void OnClick(View v) {
        if (AntiShake.check(v.getId())) {
            return;
        }
        switch (v.getId()) {
            case R.id.tv_add_gratuity:
                if (selectGratuityBean == null) {
                    showShortToast("请选择小费");
                    return;
                } else {
                    if (selectGratuityBean.getGratuity() == -1) {
                        if (TextUtils.isEmpty(ed_add_gratuity_content.getText().toString())) {
                            showShortToast("请输入小费");
                            return;
                        } else {
                            gratuity = Float.parseFloat(ed_add_gratuity_content.getText().toString());
                            Bundle bundle = new Bundle();
                            bundle.putFloat(Constants.INP_GRATUITY_VALUE, gratuity);
                            setResultOk(bundle);
                        }
                    } else {
                        gratuity = selectGratuityBean.getGratuity();
                        Bundle bundle = new Bundle();
                        bundle.putFloat(Constants.INP_GRATUITY_VALUE, gratuity);
                        setResultOk(bundle);
                    }
                }
                break;
        }
    }
}